package edu.cuit.avatar.recursive;

import java.math.BigInteger;

/**
 * 使用递归思路计算阶乘
 * 0! = 1
 * n! = n * (n - 1)!
 * @author <a href="mailto:1020zhaodan@163.com">Adan</a>
 * @version 1.0
 * @date 2025/7/14 8:48
 */
public class ComputeFactorial {

    public static BigInteger factorial(int n) {
        BigInteger result = BigInteger.ONE;
        for (int i = n; i >= 1; i--) {
            result = result.multiply(BigInteger.valueOf(i));
        }
        return result;
    }

    public static void main(String[] args) {
        System.out.println(factorial(20));
        System.out.println(factorialRecursive(20));
    }

    public static BigInteger factorialRecursive(int n) {
        BigInteger result = BigInteger.ONE;
        if (n == 0)
            return result; //基础情况
        else
            return BigInteger.valueOf(n).multiply(factorialRecursive(n - 1));
    }
}
